﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Diagnostics;
using MachineQA;
using MachineQA.Properties;

namespace MachineQA.Model
{
    public partial class Energy : IDataErrorInfo
    {
        public static Energy CreateEnergy(double value, Modality modality)
        {
            Energy retVal = new Energy();
            retVal.value = value;
            retVal.Modality = modality;
            return retVal;
        }

        #region IDataErrorInfo implementation

        public string Error
        {
            get { return null; }
        }

        public string this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo implementation

        #region Validation

        static readonly string[] ValidatedProperties =
        {
            "value",
            "Modality"
        };

        string GetValidationError(string propertyName)
        {
            if(Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "value":
                    error = this.ValidateValue();
                    break;
                case "Modality":
                    error = this.ValidateModality();
                    break;
                default:
                    Debug.Fail(Resources.UnexpectedPropertyError + propertyName);
                    break;
            }
            return error;
        }

        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        private string ValidateValue()
        {
            if (value < 0)
                return Resources.InvalidEnergyValueValidationError;

            return null;
        }

        private string ValidateModality()
        {
            if (!Constants.entities.Modalities.Contains(this.Modality))
                return Resources.ModalityDoesntExistValidationError;

            return null;
        }

        #endregion // Validation
    }
}
